package com.jvm;

/**
 * ClassName:Demo04
 * Package:com.jvm
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/9 - 14:20
 */
public class Demo04 {
    private static final int  _20M = 1024 * 1024 * 200;
    public static void main(String[] args) {
//        a(0);
        Runtime runtime = Runtime.getRuntime();
        System.out.println("运行时当前总内存：runtime.totalMemory() = " + runtime.totalMemory());
        System.out.println("runtime.totalMemory() = " + runtime.totalMemory() * 1.0 / 1024 / 1024 + " MB");

        System.out.println("jvm最大可以申请的内存：runtime.maxMemory() = " + runtime.maxMemory() * 1.0 / 1024 / 1024 + " MB");
        System.out.println("运行时剩余的空闲的内存：runtime.freeMemory() = " + runtime.freeMemory() * 1.0 / 1024 / 1024 + " MB");

//        byte[] bs = new byte[1024 * 1024 * 10];
        byte[] bs = new byte[_20M];
        //OutOfMemoryError: Java heap space OOM

        System.out.println("runtime.totaMemory() = " + runtime.totalMemory() * 1.0 / 1024 / 1024 + " MB");
        System.out.println("jvm最大可以申请的内存：runtime.maxMemory() = " + runtime.maxMemory() * 1.0 / 1024 / 1024 + " MB");
        System.out.println("运行时剩余的空闲的内存：runtime.freeMemory() = " + runtime.freeMemory() * 1.0 / 1024 / 1024 + " MB");


    }

    public static void a(int count) {
        try {
            // -sXX512kb 配置栈内存大小512KB
            System.out.println(count);
            a(++count);
        } catch (Throwable e) {
            //栈溢出错误发生时 捕获错误 打印日志
//            e.printStackTrace();
//            System.out.println("执行的次数：" + count);
        }
    }

}
